/**  定义Exercises相关的接口 */
const express = require("express");
const router = express.Router();
const Response = require("../utils/Response.js");
// 引入mysql连接池
const pool = require("../utils/db.js");

// 新增习题
router.post("/exercises/add", (req, resp) => {
  let obj = req.body
  let sql = "insert into exercises set ?"
  pool.query(sql, [obj], (error, result) => {
    if (error) {
      resp.send(Response.error(500, error));
      throw error;
    }
    // 注册成功
    resp.send(Response.ok());
  })
});

// 通过id查询习题信息
router.get("/exercises/ById",(req,resp)=>{
  let{exercise_id}=req.query
  let sql = "select * from exercises where exercise_id=?"  
  pool.query(sql,[exercise_id], (error, result)=>{
    if (error) {
      resp.send(Response.error(500, error));
      throw error;
    }
    // 查询成功 返回标签
    resp.send(Response.ok({result}));
  })
})

// 修改习题
router.post("/exercises/update", (req, resp) => {
  let obj = req.body
  let sql = "update exercises set ? where exercise_id=?";
  pool.query(sql, [obj, obj.exercise_id], (error, result) => {
    if (error) {
      resp.send(Response.error(500, error));
      throw error;
    }
    if (result.changedRows === 0) {
      resp.send(Response.error(400, '修改失败'))
    } else {
      // 修改成功
      resp.send(Response.ok());
    }
  })
});

// 删除习题
router.get("/exercises/del", (req, resp) => {
  let obj = req.query;
  let sql = "delete from exercises where exercise_id=?";
  pool.query(sql, [obj.exercise_id], (error, result) => {
    if (error) {
      resp.send(Response.error(500, error));
      throw error;
    }
    if (result.affectedRows === 0) {
      resp.send(Response.error(400, '删除失败'))
    } else {
      // 删除成功
      resp.send(Response.ok());
    }

  });
});

// 查询习题列表 分页
router.get("/exercises/list", async (req, resp) => {
  let { page, pagesize } = req.query;
  let startIndex = (page - 1) * pagesize;
  let size = parseInt(pagesize);
  try {
    let sql = "select * from exercises limit ?,?"
    let result = await pool.querySync(sql, [startIndex, size])
    let sql2 = "select count(*) as total from exercises"
    let result2 = await pool.querySync(sql2)
    resp.send(
      Response.ok({ result, total: result2[0].total, pageIndex: page, pageSize: pagesize })
    )
  }
  catch (error) {
    resp.send(Response.error(error))
  }
});

// 模糊查询习题列表
router.get("/exercises/ByTitle", async (req, resp) => {
  let { page, pagesize, title } = req.query;
  let startIndex = (page - 1) * pagesize;
  let size = parseInt(pagesize);
  try {
    let sql = `select * from exercises where exercise_text like '%${title}%' limit ?,?`;
    let result = await pool.querySync(sql, [startIndex, size]);
    let sql2 =
      `select count(*) as total from exercises where exercise_text like '%${title}%'`;
    let result2 = await pool.querySync(sql2);
    resp.send(
      Response.ok({
        result,
        total: result2[0].total,
        pageIndex: page,
        pageSize: pagesize,
      })
    );
  } catch (error) {
    resp.send(Response.error(error));
  }
});

// 查询相应习题 科目
router.get("/exercises/subject", (req, resp) => {
  let obj = req.query
  let sql = 'select * from exercises where subjectstype=?'
  pool.query(sql, [obj.subjectstype], (error, result) => {
    if (error) {
      resp.send(Response.error(500, error));
      throw error;
    }
    resp.send(Response.ok(result));
  })
});

// 通过类型查询习题列表
router.get("/exercises/Bytype", async (req, resp) => {
  let { page, pagesize, type } = req.query;
  let startIndex = (page - 1) * pagesize;
  let size = parseInt(pagesize);
  try {
    let sql = `select * from exercises where subjectstype like '%${type}%' limit ?,?`;
    let result = await pool.querySync(sql, [startIndex, size]);
    let sql2 =
      `select count(*) as total from exercises where subjectstype like '%${type}%'`;
    let result2 = await pool.querySync(sql2);
    resp.send(
      Response.ok({
        result,
        total: result2[0].total,
        pageIndex: page,
        pageSize: pagesize,
      })
    );
  } catch (error) {
    resp.send(Response.error(error));
  }
});

// 将router对象导出
module.exports = router;